package com.nus.fellowmark.modules.assignment.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.dev33.satoken.stp.StpUtil;
import com.nus.fellowmark.common.mongo.service.MongoFileService;
import com.nus.fellowmark.common.response.ServerResponseEntity;
import com.nus.fellowmark.entity.SubmissionEntity;
import com.nus.fellowmark.modules.assignment.constant.Constant;
import com.nus.fellowmark.modules.assignment.dto.DeleteByIdsDTO;
import com.nus.fellowmark.modules.assignment.dto.SubmissionDTO;
import com.nus.fellowmark.modules.assignment.dto.UploadSubmissionDTO;
import com.nus.fellowmark.modules.assignment.service.PairingService;
import com.nus.fellowmark.modules.assignment.service.SubmissionService;
import com.nus.fellowmark.modules.assignment.vo.SubmissionVO;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.nus.fellowmark.common.utils.PageUtils;
import org.springframework.web.multipart.MultipartFile;


/**
 * 
 *
 * @author Chen Ruoyi
 * @email chenruoyisz@gmail.com
 * @date 2023-02-10  
 */
@RestController
@RequestMapping("submission")
public class SubmissionController {
    @Autowired
    private SubmissionService submissionService;

    @Autowired
    MongoFileService mongoFileService;

    @Autowired
    PairingService pairingService;

    @RequestMapping("/upload")
    @ApiOperation("upload submission file")
    public ServerResponseEntity upload(@ModelAttribute UploadSubmissionDTO uploadSubmissionDTO) {
        SubmissionVO submissionVO = submissionService.upload(uploadSubmissionDTO);
        if (submissionVO == null) {
            return ServerResponseEntity.showFailMsg("Upload submission failed!");
        }

        if (submissionVO.getSubmissionFileKey().equals(Constant.FILE_EXIST)) {
            return ServerResponseEntity.showFailMsg("File exist! Delete submission before upload again!");
        }
        return ServerResponseEntity.success(submissionVO);
    }

    @ApiOperation("list submission by student id and assignment id")
    @RequestMapping("/listSubmission")
    public ServerResponseEntity listSubmissionByStuAndAsg(@RequestBody SubmissionDTO submissionDTO) {
        Object loginId = StpUtil.getLoginId();
        SubmissionEntity submission = submissionService.getByStuAndAsg(Integer.parseInt((String) loginId), submissionDTO.getAssignmentId());

        return ServerResponseEntity.success(submission);
    }

    @ApiOperation("list submission by studentId and assignmentId as path parameters")
    @RequestMapping("/listSubmission/stu/{studentId}/assignment/{assignmentId}")
    public ServerResponseEntity listSubmissionByStuIdAndAsgId(@PathVariable("studentId") Integer studentId, @PathVariable("assignmentId") Integer assignmentId) {
        SubmissionEntity submission = submissionService.getByStuAndAsg(studentId, assignmentId);

        return ServerResponseEntity.success(submission);
    }

    @ApiOperation("list submission")
    @RequestMapping("/list")
    public ServerResponseEntity list(@RequestParam Map<String, Object> params){
        PageUtils page = submissionService.queryPage(params);

        return ServerResponseEntity.success("page", page);
    }


    @ApiOperation("select submission by id ")
    @RequestMapping("/info/{id}")
    public ServerResponseEntity info(@PathVariable("id") Integer id){
		SubmissionEntity submission = submissionService.getById(id);

        return ServerResponseEntity.success("submission", submission);
    }

    @ApiOperation("save submission")
    @RequestMapping("/save")
    public ServerResponseEntity save(@RequestBody SubmissionEntity submission){
		submissionService.save(submission);

        return ServerResponseEntity.success();
    }

    @ApiOperation("update submission")
    @RequestMapping("/update")
    public ServerResponseEntity update(@RequestBody SubmissionEntity submission){
		submissionService.updateById(submission);

        return ServerResponseEntity.success();
    }

    @ApiOperation("delete submission")
    @RequestMapping("/delete")
    public ServerResponseEntity delete(@RequestBody DeleteByIdsDTO deleteByIdsDTO){
        List<Integer> submissionIds = Arrays.asList(deleteByIdsDTO.getIds());
        submissionService.removeByIds(submissionIds);
        // if the student delete his/her submission, should also turn the pairs' grading status to "inactive"
        pairingService.inactivePair(submissionIds);
        return ServerResponseEntity.success();
    }

}
